//
//  ImportFileDataController.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/11/4.
//

import UIKit
import SVProgressHUD

let kImportFileDataTitleCell = "ImportFileDataTitleCell"
let kImportFileDataCell = "kImportFileDataCell"

class ImportFileDataController: BaseTableViewController {
    
    var fileUrl: URL!
    private var fileName: String?
    private var fileExtension: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        fileName = fileUrl.lastPathComponent.lowercased()
        fileExtension = fileUrl.pathExtension
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: kImportFileDataCell)
        tableView.register(UINib(nibName: kImportFileDataTitleCell, bundle: nil), forCellReuseIdentifier: kImportFileDataTitleCell)
    }
    
    private func importIdiom() {
        guard let fileName = fileName else {
            SVProgressHUD.showError(withStatus: "导入失败")
            return
        }
        
        if !fileName.contains("成语") {
            SVProgressHUD.showError(withStatus: "请选择成语文件导入")
            return
        }
        
        if let str = getTextContent() {
            ImportTool.importIdiom(string: str)
            SVProgressHUD.showSuccess(withStatus: "导入成功")
        } else {
            SVProgressHUD.showError(withStatus: "内容为空")
        }
    }
    
    private func importShenLun() {
        guard let fileName = fileName else {
            SVProgressHUD.showError(withStatus: "导入失败")
            return
        }
        
        if fileExtension != "xlsx" {
            SVProgressHUD.showError(withStatus: "规范词语只支持.xlsx格式的文件")
            return
        }
        
        if !fileName.contains("规范") {
            SVProgressHUD.showError(withStatus: "请选择规范词语文件导入")
            return
        }
        
        if let data = getFileData() {
            ImportTool.importShenLun(data: data)
            SVProgressHUD.showSuccess(withStatus: "导入成功")
        } else {
            SVProgressHUD.showError(withStatus: "内容为空")
        }
    }
    
    private func importShenLun2() {
        guard let fileName = fileName else {
            SVProgressHUD.showError(withStatus: "导入失败")
            return
        }
        
        if fileExtension != "xlsx" {
            SVProgressHUD.showError(withStatus: "只支持.xlsx格式的文件")
            return
        }
        
        if !fileName.contains("对策") {
            SVProgressHUD.showError(withStatus: "请选择问题对策文件导入")
            return
        }
        
        if let data = getFileData() {
            ImportTool.importShenLun2(data: data)
            SVProgressHUD.showSuccess(withStatus: "导入成功")
        } else {
            SVProgressHUD.showError(withStatus: "内容为空")
        }
    }
    
    private func getFileData() -> Data? {
        _ = fileUrl.startAccessingSecurityScopedResource()
        defer {
            fileUrl.stopAccessingSecurityScopedResource()
        }
        do {
            return try Data(contentsOf: fileUrl)
        } catch {
            SVProgressHUD.showError(withStatus: "文件访问失败")
        }
        return nil
    }
    
    private func getTextContent() -> String? {
        if let data = getFileData() {
            return String(data: data, encoding: .utf8)
        }
        return nil
    }
}

extension ImportFileDataController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 100
        } else {
            return 50
        }
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: kImportFileDataTitleCell, for: indexPath) as! ImportFileDataTitleCell
            cell.titleLabel.text = fileName
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: kImportFileDataCell, for: indexPath)
            cell.accessoryType = .disclosureIndicator
            cell.textLabel?.font = UIFont.systemFont(ofSize: 16)
            if indexPath.row == 1 {
                cell.textLabel?.text =  "导入成语"
            } else if indexPath.row == 2 {
                cell.textLabel?.text =  "导入申论规范词"
            } else if indexPath.row == 3 {
                cell.textLabel?.text =  "导入申论问题对策"
            }
            return cell
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        if indexPath.row == 1 {
            importIdiom()
        } else if indexPath.row == 2 {
            importShenLun()
        } else if indexPath.row == 3 {
            importShenLun2()
        }
    }
    
}
